package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.Entity.ApJob;
import com.njworkorder.Service.ApJobService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/aoWithAp")
public class AoWithApController {

    private final ApJobService apJobService;
    public AoWithApController(ApJobService apJobService) {
        this.apJobService = apJobService;
    }


    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody ApJob ApJob){
        ApJob.setId(UUID.randomUUID().toString());
        boolean save = apJobService.save(ApJob);
        if(save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }

    @GetMapping("/getById")
    public Result<ApJob> getAoAp(@RequestParam("id") String id){
        ApJob aoWithAp = apJobService.getById(id);
        return ResultUtil.success(aoWithAp);
    }


    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id){
        boolean removeById = apJobService.removeById(id);
        if(removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @PostMapping("/updateInfo")
    public Result<String> updateInfo(@RequestBody ApJob aoWithAp){
        boolean updateById = apJobService.updateById(aoWithAp);
        if(updateById){
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.fail("更新失败");
    }


    @GetMapping("/getList")
    public Result<Page<ApJob>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize){

        LambdaQueryWrapper<ApJob> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ApJob::getStatus, 2);
        queryWrapper.orderByDesc(ApJob::getPublishTime);

        Page<ApJob> page = apJobService.page(new Page<>(pageIndex, pageSize),queryWrapper);

        return ResultUtil.success(page);
    }

}
